<?php

/* * 
 * 添加数据到数据库前对数据进行处理
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */

class content_input {

    public $modelid;
    public $fields;
    public $data;
    public $Action;
    
    /**
     * 构造函数
     * @param type $modelid 模型ID
     * @param type $Action 传入this
     */
    function __construct($modelid, &$Action = "") {
        $this->modelid = $modelid;
        $this->fields = F("Model_field_" . $modelid);
        $this->Action = $Action;
    }

    /**
     * 错误提示
     */
    public function error($message, $jumpUrl = '', $ajax = false) {
        $this->Action->error($message, $jumpUrl, $ajax);
    }

    /**
     * 数据入库前处理
     * @param type $data
     * @param type $isimport
     * @return boolean|string 
     */
    function get($data, $isimport = 0) {
        $this->data = $data;
        $info = array();
        foreach ($data as $field => $value) {
            //字段别名
            $name = $this->fields[$field]['name'];
            //最小值
            $minlength = $this->fields[$field]['minlength'];
            //最大值
            $maxlength = $this->fields[$field]['maxlength'];
            //数据校验正则
            $pattern = $this->fields[$field]['pattern'];
            //数据校验未通过的提示信息
            $errortips = $this->fields[$field]['errortips'];
            //配置
            $setting = unserialize($this->fields[$field]['setting']);

            //如果没有错误提示，默认一个
            if (empty($errortips))
                $errortips = $name . ' 不符合要求！';
            //数据长度
            $length = empty($value) ? 0 : strlen($value);
            //进行长度验证
            if ($minlength && $length < $minlength) {
                if ($isimport) {
                    return false;
                } else {
                    $this->error($name . ' 不得少于 ' . $minlength . "个字符！");
                }
            }
            if ($maxlength && $length > $maxlength) {
                if ($isimport) {
                    $value = str_cut($value, $maxlength, '');
                } else {
                    $this->error($name . ' 不得多余 ' . $maxlength . "个字符！");
                }
            } elseif ($maxlength) {
                //字符截取
                $value = str_cut($value, $maxlength, '');
            }

            if ($pattern && $length && !preg_match($pattern, $value) && !$isimport)
                $this->error($errortips);
            //模型缓存
            $MODEL = F("Model");

            //获取表名 先判断是主表还是副表
            $this->table_name = $this->fields[$field]['issystem'] ? $MODEL[$this->modelid]['tablename'] : $MODEL[$this->modelid]['tablename'] . '_data';
            //实例化数据库链接
            $this->Content = new ContentModel(ucwords($this->table_name));

            //值唯一检测
            if ($this->fields[$field]['isunique'] && ($this->Content->where(array($field => $value))->count($field)) > 0 && ACTION_NAME != 'edit')
                $this->error($name ."该值必须不重复！");
            //字段类型
            $func = $this->fields[$field]['formtype'];

            //检测对应字段方法是否存在，存在则执行此方法，并传入字段名和字段值
            if (method_exists($this, $func))
                $value = $this->$func($field, $value);
            
            //字段扩展，可以对字段内容进行再次处理，类似ECMS字段处理函数
            if($setting['backstagefun'] || $setting['frontfun']){
                load("@.treatfun");
               $backstagefun = explode("###",$setting['backstagefun']);
               $usfun = $backstagefun[0];
               $usparam = $backstagefun[1];
                //前后台
                if(isadmin == 1){
                     //检查方法是否存在
                    if(function_exists($usfun)){
                        //判断是入库执行类型
                        if( (int)$setting['backstagefun_type'] == 1 || (int)$setting['backstagefun_type'] == 3){
                            //调用自定义函数，参数传入：模型id，信息ID，字段内容，字段名，操作类型，附加参数
                            // 例子 demo($modelid ,$value , $id, $field ,$action ,$param){}
                            $id = 0;
                            @$value = call_user_func($usfun, $this->modelid ,$id ,$value ,$field ,ACTION_NAME,$usparam);
                        }
                    }
                }else{
                    //前台投稿处理自定义函数处理
                }
            }

            //把系统字段和模型字段分开
            if ($this->fields[$field]['issystem']) {
                $info['system'][$field] = $value;
            } else {
                $info['model'][$field] = $value;
            }

            //颜色选择为隐藏域 在这里进行取值
            $info['system']['style'] = $_POST['style_color'] ? strip_tags($_POST['style_color']) : '';
            //标题加粗等样式
            if ($_POST['style_font_weight'])
                $info['system']['style'] = $info['system']['style'] . ';' . strip_tags($_POST['style_font_weight']);
        }
        return $info;
    }

}?>